2d graphs

These 2d examples are both from Winsong Chang's excellent R Graphics Cookbook

Correlation Matrix Example

Correlation plot

library(corrplot)
corrplot(mcor)

plot of chunk 2d_3d_plot

Regular R output

# for the dataset
library(ggplot2)
rm(mtcars)
mcor <- cor(mtcars)
# Print mcor and round to 2 digits
round(mcor, digits = 2)
##        mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
## mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
## cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
## disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
## hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
## drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
## wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
## qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00  0.74 -0.23 -0.21 -0.66
## vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74  1.00  0.17  0.21 -0.57
## am    0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17  1.00  0.79  0.06
## gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79  1.00  0.27
## carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27  1.00

HTML table using xtable

library(xtable)
print(xtable(mcor), type = "html")
mpg cyl disp hp drat wt qsec vs am gear carb
mpg 1.00 -0.85 -0.85 -0.78 0.68 -0.87 0.42 0.66 0.60 0.48 -0.55
cyl -0.85 1.00 0.90 0.83 -0.70 0.78 -0.59 -0.81 -0.52 -0.49 0.53
disp -0.85 0.90 1.00 0.79 -0.71 0.89 -0.43 -0.71 -0.59 -0.56 0.39
hp -0.78 0.83 0.79 1.00 -0.45 0.66 -0.71 -0.72 -0.24 -0.13 0.75
drat 0.68 -0.70 -0.71 -0.45 1.00 -0.71 0.09 0.44 0.71 0.70 -0.09
wt -0.87 0.78 0.89 0.66 -0.71 1.00 -0.17 -0.55 -0.69 -0.58 0.43
qsec 0.42 -0.59 -0.43 -0.71 0.09 -0.17 1.00 0.74 -0.23 -0.21 -0.66
vs 0.66 -0.81 -0.71 -0.72 0.44 -0.55 0.74 1.00 0.17 0.21 -0.57
am 0.60 -0.52 -0.59 -0.24 0.71 -0.69 -0.23 0.17 1.00 0.79 0.06
gear 0.48 -0.49 -0.56 -0.13 0.70 -0.58 -0.21 0.21 0.79 1.00 0.27
carb -0.55 0.53 0.39 0.75 -0.09 0.43 -0.66 -0.57 0.06 0.27 1.00

Network Graph Example

library(igraph)
# Specify edges for a directed graph
gd <- graph(c(1, 2, 2, 3, 2, 4, 1, 4, 5, 5, 3, 6))
plot(gd)

plot of chunk 2d_3d_network_data

# For an undirected graph
gu <- graph(c(1, 2, 2, 3, 2, 4, 1, 4, 5, 5, 3, 6), directed = FALSE)
# No labels
plot(gu, vertex.label = NA)

plot of chunk 2d_3d_network_data

3d graphs

This example is from Yihui's response to a stack overflow question.

Need to load the CanvasMatrix library and the hook_webgl code to get 3d graphs to work

knit_hooks$set(webgl = hook_webgl)
<script src="https://dl.dropbox.com/u/15335397/misc/CanvasMatrix.js"></script>

point 3d graph

library(rgl)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x, y)
plot3d(x, y, z, col = rainbow(1000))

_2d_3d_pointssnapshot
You must enable Javascript to view this page properly.

spheres 3d graph

open3d()
## [1] 2
spheres3d(x, y, z, col = rainbow(1000))

_2d_3d_spheressnapshot
You must enable Javascript to view this page properly.

Author: Jim Hester Created: 2013 Mar 27 10:54:38 PM Last Modified: 2013 Mar 28 02:28:35 PM

Cars example

from statmethods.net

# ggplot2 examples
library(ggplot2)

# rm any local mtcars
rm(mtcars)
## Warning: object 'mtcars' not found

# use color brewer as default discrete colors
scale_colour_discrete <- function(...) scale_color_brewer(palette = "Set1", 
    ...)
scale_fill_discrete <- function(...) scale_fill_brewer(palette = "Set1", ...)

# create factors with value labels
mtcars$gear <- factor(mtcars$gear, levels = c(3, 4, 5), labels = c("3gears", 
    "4gears", "5gears"))
mtcars$am <- factor(mtcars$am, levels = c(0, 1), labels = c("Automatic", "Manual"))
mtcars$cyl <- factor(mtcars$cyl, levels = c(4, 6, 8), labels = c("4cyl", "6cyl", 
    "8cyl"))

Kernel density plots for mpg

grouped by number of gears (indicated by color)

qplot(mpg, data = mtcars, geom = "density", fill = gear, alpha = I(0.5), main = "Distribution of Gas Milage", 
    xlab = "Miles Per Gallon", ylab = "Density")

plot of chunk cars_density

Scatterplot of mpg vs. hp

for each combination of gears and cylinders in each facet, transmission type is represented by shape and color

qplot(hp, mpg, data = mtcars, shape = am, color = am, facets = gear ~ cyl, size = I(3), 
    xlab = "Horsepower", ylab = "Miles per Gallon")

plot of chunk cars_scatter

Regressions of mpg on weight

Seperate for each number of cylinders

qplot(wt, mpg, data = mtcars, geom = c("point", "smooth"), method = "lm", formula = y ~ 
    x, color = cyl, main = "Regression of MPG on Weight", xlab = "Weight", ylab = "Miles per Gallon")

plot of chunk cars_regressions

Boxplots of mpg by number of gears

observations (points) are overlayed and jittered

qplot(gear, mpg, data = mtcars, geom = c("boxplot", "jitter"), fill = gear, 
    main = "Mileage by Gear Number", xlab = "", ylab = "Miles per Gallon")

plot of chunk cars_boxplots

Author: Jim Hester Created: 2013 Mar 20 10:57:07 AM Last Modified: 2013 Mar 20 03:30:06 PM

Map plots

USA Arrests

library(ggplot2)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
library(reshape2)  # for melt
crimesm <- melt(crimes, id = 1)
require(maps)
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + 
    expand_limits(x = states_map$long, y = states_map$lat)

plot of chunk maps_arrests

last_plot() + coord_map()

plot of chunk maps_arrests

ggplot(crimesm, aes(map_id = state)) + geom_map(aes(fill = value), map = states_map) + 
    expand_limits(x = states_map$long, y = states_map$lat) + facet_wrap(~variable)

plot of chunk maps_arrests

Use fig.show='hold'

These are the same plots with fig.show='hold' in the options

ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + 
    expand_limits(x = states_map$long, y = states_map$lat)
last_plot() + coord_map()
ggplot(crimesm, aes(map_id = state)) + geom_map(aes(fill = value), map = states_map) + 
    expand_limits(x = states_map$long, y = states_map$lat) + facet_wrap(~variable)

plot of chunk maps_figshow plot of chunk maps_figshow plot of chunk maps_figshow

Author: Jim Hester Created: 2013 Mar 28 02:44:48 PM Last Modified: 2013 Mar 28 03:18:18 PM

Author: Jim Hester Created: 2013 Mar 28 03:22:28 PM Last Modified: 2013 Mar 28 03:23:17 PM